package com.smalls.newvideotwo.mvp.presenter;

import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.mvp.base.BaseBeanResult;
import com.smalls.newvideotwo.mvp.base.BasePresenter;
import com.smalls.newvideotwo.mvp.beanResult.GetColumnsResult;
import com.smalls.newvideotwo.mvp.beanResult.GetColumnsResultData;
import com.smalls.newvideotwo.mvp.http.constants.PortalErr;
import com.smalls.newvideotwo.mvp.http.constants.PortalErrUtil;
import com.smalls.newvideotwo.mvp.model.GetColumnsByTypeModel;
import com.smalls.newvideotwo.mvp.model.modelInterface.IGetColumnsModel;
import com.smalls.newvideotwo.mvp.view.IGetColumnsView;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;

import org.xutils.ex.HttpException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

public class GetColumnsPresenter extends BasePresenter<IGetColumnsView> implements BaseBeanResult.IGetColumnsResult {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "GetColumnsPresenter":"GetColumnsPresenter";
    private boolean isLive;
    private IGetColumnsModel model = new GetColumnsByTypeModel();
    private long startTime=0;
    private int categoryLevel = 0;
    private int mTotalSize = 0;
    private List<String> mCateList = null;
    private List<GetColumnsResultData> data = new ArrayList<GetColumnsResultData>();
    private GetColumnsResult mTotalResult = new GetColumnsResult();
    public void Abort() {
        if(model!=null) {
            model.Abort();
        }
    }
    public GetColumnsPresenter(IGetColumnsView view) {
        super(view);
        mTotalSize = 0;
        mTotalResult.setData(data);
    }

    public void requestData(List<String> strList, boolean isLive) {
        LogUtil.d(this.TAG, "GetColumnContentsBean");
        this.isLive = isLive;
        if(strList!=null&&strList.size()>0) {
            mTotalSize=0;
            mCateList = strList;
            this.model.getColumnContents(mCateList.get(0), this, isLive);
        }
        this.startTime = System.currentTimeMillis();
    }

    public void requestData(String strList, boolean isLive) {
        LogUtil.d(this.TAG, "GetColumnContentsBean");
        this.isLive = isLive;

        mTotalSize=0;
        mCateList = new ArrayList<>();
        mCateList.add(strList);
        this.model.getColumnContents(mCateList.get(0), this, isLive);

        this.startTime = System.currentTimeMillis();
    }

    public void requestData(boolean isLive) {
        LogUtil.d(this.TAG, "GetColumnContentsBean");
        this.isLive = isLive;
        this.model.getColumnContents(null,this, isLive);
        this.startTime = System.currentTimeMillis();
        data.clear();
        mTotalResult.getData().clear();
    }
    public void onError(Throwable e) {
        e.printStackTrace();
        String errCode = "";
        if ((e instanceof TimeoutException) || (e instanceof ConnectException) || (e instanceof SocketTimeoutException)||(e instanceof java.net.UnknownHostException)) {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB2");

            if (!NewDTVApp.getApp().isLastServer()) {
                NewDTVApp.getApp().setmEpgServerNext();
                if(mCateList!=null&&mCateList.size()>0) {
                    model.getColumnContents(mCateList.get(0), this, isLive);
                } else {
                    requestData(false);
                }
                return ;
            }
            errCode = PortalErr.STR_BOX_EPG_TIMEOUT_ERROR;
        } else {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB3");
            errCode = PortalErr.PORTAL_SYSTEM;
        }
        PortalErrUtil.getInstance().setErrCode(errCode);
        ((IGetColumnsView) this.view).showErrDialogGetColumnContents(errCode, categoryLevel);
        if (e instanceof HttpException) {

        }
        final long resposeTime = System.currentTimeMillis() - this.startTime;
    }

    @Override
    public void onNext(GetColumnsResult result) {
        PortalErrUtil.getInstance().setErrCode("");
        if (result == null || result.getData() == null|| result.getData().size() <= 0) {
            LogUtil.e(this.TAG, "\u663e\u793aDialog\u4fe1\u606fEB1");
            PortalErrUtil.getInstance().setErrCode(PortalErr.PORTAL_ROOT_COLUMN);
            ((IGetColumnsView) this.view).showErrDialogGetColumnContents(PortalErr.PORTAL_ROOT_COLUMN, this.categoryLevel);
        } else {
            mTotalResult.getData().addAll(result.getData());
            mTotalSize++;
            LogUtil.d(this.TAG, this.TAG + "  onNext mTotalSize="+mTotalSize+" result=" + result.toString());
            result.getData().clear();
            if(mCateList!=null&&mTotalSize<mCateList.size()) {
                model.getColumnContents(mCateList.get(mTotalSize),this,isLive);
                return;
            }
            ((IGetColumnsView) this.view).onSucceedGetColumnContents(mTotalResult, this.categoryLevel);
        }
    }
}
